package com.cnlbc.View;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import com.cnlbc.Dao.BookTypeDao;
import com.cnlbc.Model.BookType;
import com.cnlbc.Util.DbUtil;
import com.cnlbc.Util.StringUtil;
import java.awt.Font;

public class AddType extends JInternalFrame {
	private JTextField bookTypename;
	private JTextArea bookTypedesc;
	private DbUtil dbUtil=new DbUtil(); 
	private BookTypeDao bookTypedao=new BookTypeDao();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddType frame = new AddType();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddType() {
		setIconifiable(true);
		setClosable(true);
		setTitle("图书类别添加");
		setBounds(100, 100, 524, 394);
		
		JLabel lblNewLabel = new JLabel("图书类别名称:");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 18));
		
		bookTypename = new JTextField();
		bookTypename.setFont(new Font("宋体", Font.PLAIN, 18));
		bookTypename.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("图书类别描述：");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 18));
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addbookType(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(AddType.class.getResource("/images/add.png")));
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetAction(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(AddType.class.getResource("/images/reset.png")));
		
		bookTypedesc = new JTextArea();
		bookTypedesc.setFont(new Font("MV Boli", Font.PLAIN, 18));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(64)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(bookTypedesc, GroupLayout.PREFERRED_SIZE, 244, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel)
									.addGap(33)
									.addComponent(bookTypename, GroupLayout.PREFERRED_SIZE, 244, GroupLayout.PREFERRED_SIZE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(107)
							.addComponent(btnNewButton)
							.addGap(114)
							.addComponent(btnNewButton_1)))
					.addContainerGap(78, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(85)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel)
							.addGap(33)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_1)
								.addComponent(bookTypedesc, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
							.addGap(41)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnNewButton_1)
								.addComponent(btnNewButton)))
						.addComponent(bookTypename, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(187))
		);
		getContentPane().setLayout(groupLayout);
		bookTypedesc.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
	}

	private void addbookType(ActionEvent evt) {
		String booktypename=this.bookTypename.getText();
		String booktypedesc=this.bookTypedesc.getText();
		if(StringUtil.isEmpty(booktypename)||StringUtil.isEmpty(booktypedesc)) {
			JOptionPane.showMessageDialog(null, "填写内容不能有空");
			return;
		}else {
			Connection con=null;
			try {
				con=dbUtil.getCon();
				BookType bookType=new BookType(booktypename,booktypedesc);
				int n=bookTypedao.add(con, bookType); 
				if(n==1) {
					JOptionPane.showMessageDialog(null, "添加成功");
					this.bookTypename.setText("");
					this.bookTypedesc.setText("");
					dispose();
				}else {
					JOptionPane.showMessageDialog(null, "添加失败");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					dbUtil.close(con);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	private void resetAction(ActionEvent evt) {
		// TODO Auto-generated method stub
		this.bookTypename.setText("");
		this.bookTypedesc.setText("");
	}
}
